package cn.itsource.hbc.product.controller;

import cn.itsource.hbc.product.domain.Product;
import cn.itsource.hbc.product.query.ProductQuery;
import cn.itsource.hbc.product.service.IProductService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product){

        try {
            if(product.getId()!=null){
                productService.update(product);
            }else {
                productService.save(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }

    }



    @GetMapping
    public List<Product> loadAll(){

        return productService.loadAll();
    }


    @GetMapping("/{id}")
    public Product loadById(@PathVariable("id") Long id){

        return productService.loadById(id);

    }

    @GetMapping("/detail/{id}")
    public Product loadByIdOfFront(@PathVariable("id") Long id){

        return productService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }

    }

    @PostMapping
    public PageResult<Product> queryPage(@RequestBody ProductQuery query){

        return  productService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult allDelete(@RequestBody Long[]ids){
        try {
            productService.allDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }


    }



    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            productService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }


    @PostMapping("/front")
    public PageResult<Product> queryPageOfFront(@RequestBody ProductQuery query ){
        return productService.queryPage(query);
    }

}
